<html>
	<head>
		<title>JSEV Type Test</title>
		<script type="application/javascript" src="../methods.Object.js"></script>
		<script type="application/javascript" src="../methods.Array.js"></script>
		<script type="application/javascript" src="../JSON.schema.reg.js"></script>
		<script type="application/javascript" src="../JSON.path.js"></script>
		<script type="application/javascript" src="../JSON.schema.err.js"></script>
		<script type="application/javascript" src="../JSON.schema.err.messages.en.js"></script>
		<script type="application/javascript" src="../JSON.schema.ext.walker.js"></script>
		<script type="application/javascript" src="../JSON.schema.ext.js"></script>
		<script type="application/javascript">
			function validTest() {
				var schema1 = {},
					schema0 = {"type": schema1},
					schema3 = {"id": "schema3", "type": "string"},
					schema2 = {"type": "schema3", },
					schema4 = {"type": {"$ref" : "schema3"}},
					schema5 = {"type": ["schema3", "string"]},
					schema6 = JSON.schema.registry.integer,
					schema7 = {
						"type": [
							{"type": "string", "minLength": 5},
							{"type": "string", "minLength": 8}
						],
						"extends": "schema3"
					},
					schemasToValidate = [schema0, schema2, schema4, schema6, schema7],
					retVal = undefined;

				try {
					JSON.schema.registry.unregister("*");
					JSON.schema.registry.register(schema3);
				} catch (e) {
					console.error("Type registration failed: ", e);
				}

				for (var i in schemasToValidate) {
					retVal = JSON.schema.ext.validate(schemasToValidate[i]);
					if (retVal.valid) {
						console.info("Test " + i + " passed!", retVal);
					} else {
						console.warn("Test " + i + " failed.", retVal);
						if (retVal.errors.length > 0) {
							console.group("Error messages:");
							for (var i in retVal.errors) {
								console.info(JSON.schema.err.localize("en", retVal.errors[i]));
							}
							console.groupEnd();
						} else {
							console.info("No errors reported.");
						}
					}
				}
			}

			function invalidTest() {
				var schema0 = {"type": "badName"},
					schema1 = {"type": true},
					schema2 = {"type": false},
					schema3 = {"type": undefined},
					schema4 = {"type": null},
					schema5 = {"type": 23},
					/* number is not a subtype of integer */
					schema6 = {"type": "number", "extends": "integer"},
					schema7 = { "type": ["badName", "string"] },
					schema8 = { "type": ["string", true] },
					schema9 = { "type": [false, "string"] },
					schema10 = { "type": ["string", undefined] },
					schema11 = { "type": [null, "string"] },
					schema12 = { "type": ["string", 23] },
					/* number and string are not subtypes of integer */
					schema13 = { "type": ["number", "string"], "extends": "integer"},
					errorsExpected = [
						["IVE000"], ["TE000"], ["TE000"], ["TE000"], ["TE000"],
						["TE000"], ["STE000"], ["IVE000"], ["TE000"],
						["TE000"], ["TE000"], ["TE000"], ["TE000"], ["STE000", "STE000"]
					],
					schemasToValidate = [
						schema0,  schema1,  schema2,  schema3,  schema4,
						schema5,  schema6,  schema7,  schema8,
						schema9, schema10, schema11, schema12, schema13
					],
					retVal = undefined;

				function correctErrors(vRetVal, errorsExpected) {
					if (vRetVal.errors.length != errorsExpected.length)
						return false;

					var i = 0;
					for (; i < errorsExpected.length; i++) {
						if (errorsExpected[i] != vRetVal.errors[i].code) {
							return false;
						}
					}
					return true;
				}

				for (var i in schemasToValidate) {
					retVal = JSON.schema.ext.validate(schemasToValidate[i]);
					if (!retVal.valid && correctErrors(retVal, errorsExpected[i])) {
						console.info("Test " + i + " passed!", retVal);
						if (document.getElementById("showInvMsg").checked) {
							console.group("Error Messages for expected errors:");
							for (var j in retVal.errors)
								console.info(JSON.schema.err.localize("en", retVal.errors[j]));
							console.groupEnd();
						}
					} else {
						console.warn("Test " + i + " failed.", retVal);
						console.group("Reason: ");
						var loops = errorsExpected[i].length > retVal.errors.length ?
							errorsExpected[i].length : retVal.errors.length;
						var msg = "";

						for (var j = 0; j < loops; j++) {
							if (
								j in errorsExpected[i] && j in retVal.errors &&
								errorsExpected[i][j] == retVal.errors[j].code
							) continue;

							if (j in errorsExpected[i])
								msg += "Error Expected: " + errorsExpected[i][j];
							else
								msg += "No error expected";

							if (j in retVal.errors)
								msg += ", found: " + retVal.errors[j].code + ".";
							else
								msg += ", no error found."

							console.info(msg);
							msg = "";
						}
						console.groupEnd();
					}
				}
			}
		</script>
	</head>
	<body>
		<div> Tests the `type' restriction check of JSON.schema.ext.validate </div>
		<div><button onclick="validTest();">Valid Schema Test</button></div>
		<div><button onclick="invalidTest();">Invalid Schema Test</button><input type="checkbox" id="showInvMsg"></input><label for="showInvMsg">Show Messages</label></div>
	</body>
</html>